/***
 * 流程业务标识js
 */
$(function () {

    //初始化加载datagrid
    $('#dg1').datagrid({
        method: 'post',
        url: ROOT_PATH + '/workflow/flowBusiness/findFlowBusinessList.shtml',
        /*queryParams: {
            sort: 'sort',
            order: 'desc',
            deptCode: deptCode
        },*/
        //idField: 'id',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'id', width: 80, checkbox: true},
            {field: 'identity', title: '业务标识', width: 100, sortable: true},
            {field: 'businessName', title: '业务名称', width: 80, sortable: true},
            {field: 'sort', title: '排序', width: 100},
            {field: 'description', title: '备注', width: 100}
        ]]
    });
    /***
     * 查询按钮事件
     */
    $('#btn_search').on("click", function () {
        $('#dg1').datagrid('load', $('#tb1').find(':input').serializeJson());
    });

    /**
     * 打开业务标识创建页面
     * @Author: wangqingguo
     * @Date: 2017/3/14
     */
    $('#btn_workflow_flowBusiness_add').on('click',function(){
        var index = layer.open({
            type: 2,
            title: '添加业务标识',
            area: ['650px', '80%'],
            content: ROOT_PATH + '/workflow/flowBusiness/add.shtml'
        });
    });
    /***
     * 打开流程业务信息编辑页面
     * @returns {boolean}
     */

    $('#btn_workflow_flowBusiness_edit').on('click',function(){
        var $selected = $('#dg1').datagrid('getSelections');
        if ($selected.length != 1) {
            layer.alert('请选择一个流程业务！');
            return false;
        }
        var index = layer.open({
            type: 2,
            title: '编辑用户',
            area: ['600px', '80%'],
            content: ROOT_PATH + '/workflow/flowBusiness/edit.shtml?businessIdentity=' + $selected[0].identity
        });
    });
    $("#btn_workflow_flowBusiness_delete").on('click',function(){
        var $selected = $('#dg1').datagrid('getSelections');
        if ($selected.length != 1) {
            layer.alert('请选择一个流程业务！');
            return false;
        }
        //验证业务标识是否已经加入工作流
        var result = $.ajax({
            async : false,
            cache : false,
            type : 'post',
            url : ROOT_PATH+'/workflow/flowBusiness/checkBusinessCanDelete.shtml',
            data : {
                identity:$selected[0].identity
            }
        }).responseText;
        var dataObj=$.parseJSON(result);//转换为json对象
        var flag=dataObj.success==true;
        if(flag){
            layer.confirm('是否确定删除当前选中数据？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    type:'post',
                    url : ROOT_PATH+'/workflow/flowBusiness/deleteFlowBusiness.shtml',
                    data :{id:$selected[0].id},
                    dataType:"json",
                    success: function (result) {
                        if (result.success) {
                            $('#dg1').datagrid('clearSelections').datagrid('load');
                            layer.msg('删除成功', {
                                icon: 1,
                                time: 1000
                            });
                        }
                        else {
                            layer.msg('删除失败', {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                });

            });
        }else{
            layer.alert('该业务标识已加入工作流，不可删除！');
        }

    });

});